import { firstMent } from './../utils/mapMnent'
import { localCache } from '@/utils/cache'
import { createRouter, createWebHashHistory } from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/main'
    },
    {
      // 登陆页面
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/Login.vue')
    },
    {
      // 主页面
      path: '/main',
      name: 'main',
      component: () => import('@/views/main/Main.vue'),
      meta: {
        islogin: true
      }
    },
    {
      // NotFound页面
      path: '/:pathMath(.*)',
      name: 'NotFound',
      component: () => import('@/views/not-found/NotFound.vue')
    }
  ]
})

router.beforeEach((to, form) => {
  const token = localCache.getCache('token')
  if (to.path.startsWith('/main') && !token) {
    return '/login'
  }
  if (to.path === '/main' && token) {
    return firstMent?.url
  }
})

export default router
